package com.kornero.common.app;

import android.content.Context;
import android.content.SharedPreferences;
import org.apache.commons.lang3.StringUtils;

class PreferencesActivity extends ExtrasActivity {

    private static final int DEFAULT_MODE = Context.MODE_PRIVATE;

    protected String getPreferenceString(final String name) {
        return getPreferenceString(null, name);
    }

    protected String getPreferenceString(final String preferencesName, final String name) {
        return getPreferenceString(preferencesName, name, StringUtils.EMPTY);
    }

    protected String getPreferenceString(final String preferencesName, final String name, final String defaultValue) {
        final SharedPreferences settings = getSharedPreferences(preferencesName);
        return settings.getString(name, defaultValue);
    }

    protected Long getPreferenceLong(final String name) {
        return getPreferenceLong(null, name);
    }

    protected Long getPreferenceLong(final String preferencesName, final String name) {
        return getPreferenceLong(preferencesName, name, 0L);
    }

    protected Long getPreferenceLong(final String preferencesName, final String name, final Long defaultValue) {
        final SharedPreferences settings = getSharedPreferences(preferencesName);
        return settings.getLong(name, defaultValue);
    }

    protected void setPreferenceString(final String name, final String value) {
        setPreferenceString(null, name, value);
    }

    protected void setPreferenceString(final String preferencesName, final String name, final String value) {
        final SharedPreferences settings = getSharedPreferences(preferencesName);
        final SharedPreferences.Editor editor = settings.edit();
        editor.putString(name, value);
        editor.commit();
    }

    protected void setPreferenceLong(final String name, final Long value) {
        setPreferenceLong(null, name, value);
    }

    protected void setPreferenceLong(final String preferencesName, final String name, final Long value) {
        final SharedPreferences settings = getSharedPreferences(preferencesName);
        final SharedPreferences.Editor editor = settings.edit();
        editor.putLong(name, value);
        editor.commit();
    }

    protected void clearPreferences() {
        clearPreferences(null);
    }

    protected void clearPreferences(final String preferencesName) {
        final SharedPreferences settings = getSharedPreferences(preferencesName);
        final SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
    }

    protected SharedPreferences getSharedPreferences(final String preferencesName) {
        return getSharedPreferences(preferencesName, DEFAULT_MODE);
    }

    @Override
    public SharedPreferences getSharedPreferences(final String preferencesName, final int mode) {
        if (StringUtils.isNotEmpty(preferencesName)) {
            return super.getSharedPreferences(preferencesName, mode);
        } else {
            return getPreferences(mode);
        }
    }
}